package cn.zn.modules.zzsys.cur.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.zn.framework.common.vo.CodeNameVO;
import cn.zn.framework.common.vo.ComboxItemVO;
import cn.zn.framework.redis.RedisKeys;
import cn.zn.framework.redis.RedisUtils;
import cn.zn.modules.zzsys.cur.dao.TSysDicDataMapper;
import cn.zn.modules.zzsys.cur.entity.TSysDicData;
import cn.zn.modules.zzsys.cur.entity.TSysDicType;
import cn.zn.modules.zzsys.inter.SysDicInter;
import cn.zn.modules.zzsys.cur.service.TSysDicDataService;
import cn.zn.modules.zzsys.cur.service.TSysDicTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beust.jcommander.internal.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zn
 */
@Service
public class TSysDicDataServiceImpl extends ServiceImpl<TSysDicDataMapper, TSysDicData> implements TSysDicDataService, SysDicInter {

    @Autowired
    private TSysDicTypeService tSysDicTypeService;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<ComboxItemVO> queryByType(String typeCode) {
        return baseMapper.queryByType(typeCode).stream().map(o -> ComboxItemVO.builder()
                .value(o.getCode())
                .text(o.getValue())
                .build()).collect(Collectors.toList());
    }

    @Override
    public List<CodeNameVO> querySysDicDatas(String dicTypeCode) {
        List<CodeNameVO> dics = null;
        String cacheStr = redisUtils.get(RedisKeys.getSysDicKey(dicTypeCode));
        if (cacheStr == null) {
            TSysDicType tSysDicType = tSysDicTypeService.queryByCode(dicTypeCode);
            dics = super.list(new QueryWrapper<TSysDicData>()
                    .eq("DIC_TYPE_", tSysDicType.getId())
                    .orderByAsc("CODE_"))
                    .stream().map(o -> CodeNameVO.builder()
                            .code(o.getCode())
                            .name(o.getValue())
                            .build()).collect(Collectors.toList());
            redisUtils.set(RedisKeys.getSysDicKey(dicTypeCode), dics);
        } else {
            dics = JSONUtil.toList(JSONUtil.parseArray(cacheStr), CodeNameVO.class);
        }
        return dics;
    }

    @Override
    public List<CodeNameVO> saveSysDicDatas(String dicTypeCode, List<String> values) {
        if (values == null || values.isEmpty()) {
            return Lists.newArrayList();
        }
        // 得到字典类型
        TSysDicType tSysDicType = tSysDicTypeService.queryByCode(dicTypeCode);
        // 得到最大编号
        TSysDicData tSysDicData = super.getOne(new QueryWrapper<TSysDicData>()
                .eq("DIC_TYPE_", tSysDicType.getId())
                .select("MAX(CODE_) AS CODE_"));
        List<Integer> maxCodes = Lists.newArrayList();
        maxCodes.add(tSysDicData == null ? 0 : Integer.valueOf(tSysDicData.getCode()));
        List<TSysDicData> tSysDicDatas = values.stream().map(o -> {
            String code = this.getCode(maxCodes.get(0));
            maxCodes.set(0, Integer.valueOf(code));
            return TSysDicData.builder()
                    .dicType(tSysDicType.getId())
                    .code(code)
                    .name(code)
                    .value(o)
                    .build();
        }).collect(Collectors.toList());
        super.saveBatch(tSysDicDatas);
        // 移除缓存中数据
        redisUtils.delete(RedisKeys.getSysDicKey(dicTypeCode));
        return tSysDicDatas.stream().map(o -> CodeNameVO.builder()
                .code(o.getCode())
                .name(o.getValue())
                .build()).collect(Collectors.toList());
    }

    private String getCode(int maxCode) {
        String code = StrUtil.fillBefore(String.valueOf(maxCode + 1), '0', 3);
        return code;
    }
}
